package com.lin.project.cloudnative.aop;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.lin.project.cloudnative.annotation.AuthCheck;
import com.lin.project.cloudnative.common.ErrorCode;
import com.lin.project.cloudnative.exception.BusinessException;
import com.lin.project.cloudnative.model.entity.User;
import com.lin.project.cloudnative.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 权限校验 AOP
 * @author: linhuaixu
 * @time: 2023/5/15 20:52
 */
@Aspect
@Component
public class AuthInterceptor {
    @Resource
    private UserService userService;

    @Around("@annotation(authCheck)")
    public Object doInterceptor(ProceedingJoinPoint joinPoint, AuthCheck authCheck) throws Throwable{
        List<String> anyRole = Arrays.stream(authCheck.anyRole()).filter(StringUtils::isNotBlank).collect(Collectors.toList());
        String mustRole= authCheck.mustRole();
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        //当前登录用户
        User user = userService.getLoginUser(request);
        //拥有任意权限即通过
        if(CollectionUtils.isNotEmpty(anyRole)){
            String userRole=user.getUserRole();
            if(!anyRole.contains(userRole)){
                throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
            }
        }
        //必须有所有权限才通过
        if(StringUtils.isNotBlank(mustRole)){
            String userRole=user.getUserRole();
            if(!mustRole.equals(userRole)){
                throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
            }
        }
        //通过权限校验，放行
        return joinPoint.proceed();
    }

}
